/**
 *
 * @author ace
 * @create 2017/7/27
 */
class TextTips extends ComponentEx {

    private lab_desc: eui.Label;

    private target: egret.DisplayObject;
    //偏移量
    public offsetPos: { x: number, y: number };

    constructor(target: egret.DisplayObject) {
        super();
        this.skinName = "TextTipsSkin";
        this.offsetPos = null;
        this.target = target;
        this.target.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchHandler, this);
        this.target.addEventListener(egret.TouchEvent.TOUCH_END, this.touchHandler, this);
        this.target.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchHandler, this);
        this.target.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.touchHandler, this);

        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.touchHandler, this);
    }

    childrenCreated() {
        super.childrenCreated();
    }

    private touchHandler(e: egret.TouchEvent) {
        switch (e.type) {
            case egret.TouchEvent.TOUCH_BEGIN:
                this.show();
                break;
            case egret.TouchEvent.TOUCH_MOVE:
                egret.setTimeout(this.hide, this, 1000);
                break;
            case egret.TouchEvent.TOUCH_END:
            case egret.TouchEvent.TOUCH_RELEASE_OUTSIDE:
                this.hide();
                break;
        }
    }

    set desc(value: string) {
        this.lab_desc.text = value;
        this.lab_desc.width = this.lab_desc.width > 400 ? 400 : this.lab_desc.width;
    }

    show() {
        super.show();

        if (this.target) {
            const pos: egret.Point = this.target.localToGlobal(0, 0);
            var lessWidth: number = pos.x < acelab.width / 2 ? 0 : this.width;
            this.x = pos.x + 20 - lessWidth;
            this.y = pos.y - this.height - 10;
            if (this.offsetPos) {
                this.x = pos.x + this.offsetPos.x - lessWidth;
                this.y = pos.y + this.offsetPos.y;
            }
        }

        gameManager.layerManager.topLayer.addChild(this);
    }

    hide() {
        super.hide();

        if (gameManager.layerManager.topLayer.contains(this)) {
            gameManager.layerManager.topLayer.removeChild(this);
        }
    }
}